Ext.define('YXMES.sbas.controller.SupplierController', {
	extend : 'Ext.app.Controller',
	refs : [ {
		selector : 'sbas_supplier',
		ref : 'sbassupplier'
	} ],
	requires : [ 'YXMES.sbas.store.SupplierStore' ],
	stores : [],
	models : [],
	views : [ 'YXMES.sbas.view.SupplierView' ],
	subcontrollers : [  ],
	_init : false,
	init : function() {
		if (this._init)
			return;
		this._init = true;
		this.control({
			'sbas_supplier' : {
				afterrender : function() {
					return this.handle(this.onAfterrender, arguments);
				},
				destroy : function() {
					return this.handle(this.onDestroy, arguments);
				}
			},
			'sbas_supplier #btnSearch' : {
				click : function() {
					return this.handle(this.onBtnSearchClick, arguments);
				}
			},
			'sbas_supplier #btnSave' : {
				click : function() {
					return this.handle(this.onBtnSaveClick, arguments);
				}
			},
			'sbas_supplier #btnDel' : {
				click : function() {
					return this.handle(this.onBtnDelClick, arguments);
				}
			},
			'sbas_supplier #btnReset' : {
				click : function() {
					return this.handle(this.onBtnResetClick, arguments);
				}
			},
			'sbas_supplier #grdSup' : {
				itemclick : function() {
					return this.handle(this.onGrdSupItemclick, arguments);
				}
			}
		});
		if (!Ext.isEmpty(this.subcontrollers)) {
			Ext.each(this.subcontrollers, function(name) {
				this.getController(name).init();
			}, this);
		}
		// User definied init
	},
	mainView : undefined,
	getMainView : function() {
		return this.mainView;
	},
	getScopeView : function() {
		return this.getMainView().scopeView || {};
	},
	getInput : function() {
		return this.getScopeView().input || {};
	},
	setInput : function(data) {
		this.getMainView().input = data;
	},
	getOutput : function() {
		return this.getMainView().output || {};
	},
	setOutput : function(data) {
		this.getScopeView().output = data;
	},
	handle : function(func, args) {
		var item = args && args.length > 0 && Ext.typeOf(args[0]) === 'object' ? args[0] : undefined;
		if (item) {
			try {
				this.mainView = (item.xtype == 'sbas_supplier' ? item : item.up('sbas_supplier'));
			} catch(e) {
			}
		}
		try {
			return func.apply(this, args);
		} finally {
		}
	},
	getItem : function(itemId) {
		return itemId===this.getMainView().getItemId()?this.getMainView():this.getMainView().down('#' + itemId);
	},
	getStore : function(name) {
		if (name == 'SupplierStore')
			return this.getMainView().SbasViewSupplierRosterServiceStore;
		return undefined;
	},
	bind : function(view) {
		view = (view ? view : this.getMainView());
		view.SbasViewSupplierRosterServiceStore = Ext.create('YXMES.sbas.store.SupplierStore', {
			pageSize : 50
		});
		view.down('#grdSup').reconfigure(view.SbasViewSupplierRosterServiceStore);
		view.down('#bar3').bindStore(view.SbasViewSupplierRosterServiceStore);
	},
	onAfterrender : function(view, eOpts) {
		this.bind();
		// User defined afterrender
	},
	onDestroy : function(view, eOpts) {
		// User defined destory
		var view = this.getMainView();
		if (view.SbasViewSupplierRosterServiceStore) {
			view.SbasViewSupplierRosterServiceStore.destroyStore();
			view.SbasViewSupplierRosterServiceStore = undefined;
		}
		if (this.getMainView().input)
			this.getMainView().input = undefined;
		if (this.getMainView().output)
			this.getMainView().output = undefined;
	},
	onBtnSearchClick : function(button, e, eOpts) {
		this.loadSupplierStore(this.getItem('frmSearch').getValues());
	},
	onBtnSaveClick : function(button, e, eOpts) {
		var vals = this.getItem('frmOper').getValues();
		if (Ext.isEmpty(vals.supName)) {
		    return;
		}
		
		var me = this;
		vals["status"] = 'N';
		
		Ext.Msg.show({
		    title: T.label.question,
		    msg: T.msg.are_you_sure_to_save_data,
		    icon: Ext.Msg.QUESTION,
		    buttons: Ext.Msg.YESNO,
		    fn: function(btnId) {
		        if ('yes' == btnId) {
		            button.disable();
		
		            YX.ZbusRpc.request({
		                module: 'SbasSaveSupplierRosterService',
		                params: vals,
		                showSuccess: true,
		                callback: function() {
		                    button.enable();
		                },
		                success: function(rsp) {
		                    me.loadSupplierStore();
		                    me.getItem('frmOper').getForm().reset();
		                }
		            });
		        }
		    }
		});
	},
	onBtnDelClick : function(button, e, eOpts) {
		var vals = this.getItem('frmOper').getValues();
		if (Ext.isEmpty(vals.supCode)) {
		    return;
		}
		
		var me = this;
		vals["status"] = 'D';
		
		Ext.Msg.show({
		    title: T.label.question,
		    msg: T.msg.are_you_sure_to_delete_data,
		    icon: Ext.Msg.QUESTION,
		    buttons: Ext.Msg.YESNO,
		    fn: function(btnId) {
		        if ('yes' == btnId) {
		            button.disable();
		
		            YX.ZbusRpc.request({
		                module: 'SbasSaveSupplierRosterService',
		                params: vals,
		                showSuccess: true,
		                callback: function() {
		                    button.enable();
		                },
		                success: function(rsp) {
		                    me.loadSupplierStore();
		                    me.getItem('frmOper').getForm().reset();
		                }
		            });
		        }
		    }
		});
	},
	onBtnResetClick : function(button, e, eOpts) {
		this.getItem('frmOper').getForm().reset();
	},
	onGrdSupItemclick : function(dataview, record, item, index, e, eOpts) {
		this.getItem('frmOper').loadRecord(record);
	},
	loadSupplierStore : function(params, options) {
		params = Ext.applyIf(params||{}, {
			// Default params
		});
		
		var store = this.getStore('SupplierStore');
		if(!store) {
			return false;
		}
		
		store.getProxy().extraParams = params;
		if(store.loadPage){
			store.loadPage(1, options);
		}else{
			store.load(options);
		}
	}
});